package com.obs.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.S3Object;
import com.obs.config.S3Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@ResponseBody
public class ObsController {
    @Autowired
    private S3Properties properties;

    @Resource
    private AmazonS3 client;

    @GetMapping("/oss")
    public void test3() {
        String bucketName = properties.getBucketName();
        if (client.doesBucketExistV2(bucketName)) {
            System.out.println("s3桶 bucket:" + bucketName + " 已存在,不需要新建桶=============");
        }

        String key = UUID.fastUUID() + ".csv";
        System.out.println(key);
        client.putObject(bucketName, key, FileUtil.file("C:\\Users\\fulls\\Desktop\\经典data_X.csv"));
        S3Object object = client.getObject(bucketName, key);
        FileUtil.writeBytes(IoUtil.readBytes(object.getObjectContent()), "D:\\WorkSpace\\easyexcel\\minioClient\\oss数据.csv");
    }

    @GetMapping("/oss_download")
    public void test2() {
        String bucketName = properties.getBucketName();
        if (client.doesBucketExistV2(bucketName)) {
            System.out.println("s3桶 bucket:" + bucketName + " 已存在,不需要新建桶=============");
        }

        String key = "f06127536f0211ed909b02000a08038d.csv";

        S3Object object = client.getObject(bucketName, key);
        FileUtil.writeFromStream(object.getObjectContent(), "D:\\WorkSpace\\easyexcel\\minioClient\\oss数据.csv");


    }


}
